myApp.controller('adminListCtrl',function($scope,$serAdmin,$uibModal,$log,toaster,$localStorage) {

    $scope.vm = {
        data: {},
        page: {}
    };

    $scope.model = {
        info: {
            state: 0,
            id: '',
            name: '',
            phone: '',
            enable: false
        },
        open: function (state, data) {
            if (state == 0) {
                $scope.model.info = {
                    state: state,
                    workerId: '',
                    name: '',
                    phone: '',
                    password: '',
                    roleId: '',
                    enable: 0
                }
            }
            if (state == 1 && data) {
                $scope.model.info = {
                    state: state,
                    workerId: data.id,
                    name: data.name,
                    phone: data.phone,
                    password: '',
                    enable: Number(data.enable),
                    roleId: data.roleId
                }
            }
            var modalInstance = $uibModal.open({
                templateUrl: 'adminModalContent.html',
                controller: 'adminModalCtrl',
                windowClass: 'adminOpen',
                resolve: {
                    info: function () {
                        return $scope.model.info;
                    }
                }
            });
            modalInstance.result.then(function (res) {
                if (res) {
                    if (state == 0) {
                        $serAdmin.add({
                            token : $localStorage.userInfo&&$localStorage.userInfo.token,
                            phone: res.phone,
                            password: res.password,
                            name: res.name,
                            roleId: res.roleId,
                            enable: res.enable
                        }).then(function (res) {
                            var type = '';
                            switch (res.code) {
                                case 1:
                                    type = 'success';
                                    break;
                                default:
                                    type = 'error';
                                    break;
                            }
                            toaster.pop(type, '信息', res.message);
                            init();
                        })
                    }
                    if (state == 1) {
                        $serAdmin.save({
                            token : $localStorage.userInfo&&$localStorage.userInfo.token,
                            workerId: res.workerId,
                            phone: res.phone,
                            password: res.password,
                            name: res.name,
                            roleId: res.roleId,
                            enable: res.enable
                        }).then(function (res) {
                            var type = '';
                            switch (res.code) {
                                case 1:
                                    type = 'success';
                                    break;
                                default:
                                    type = 'error';
                                    break;
                            }
                            toaster.pop(type, '信息', res.message);
                            init();
                        })
                    }
                }
            }, function () {
                $log.info('Modal dismissed at: ' + new Date());
            });
        }
    };

    /*跳转页面*/
    $scope.pageChanged = function() {
        init($scope.vm.page.currentpage-1)
    };
    /*每页显示数量显示*/
    $scope.placement = {
        options: [5,10,20,50],
        pageSize:5,
        selected: 10,
        changeFn:function(){
            init(0)
        }
    };
    $scope.deltaU =function(id){
        $serAdmin.del({
            token : $localStorage.userInfo&&$localStorage.userInfo.token,
            workerId:id
        }).then(function(res){
            if(res.code==1){
                toaster.success('提示',res.message);
                init();
            }else{
                toaster.info('提示',res.message);
            }
        })
    };
    /*初始化数据*/
    function init(page){

        $serAdmin.list({
            token : $localStorage.userInfo&&$localStorage.userInfo.token,
            page: page||0,
            limit: $scope.placement.selected
        }).then(function (res) {
            $scope.vm.data = res.data;
            if(!page||page==0){$scope.vm.page = res.addtion_data;}
        });
    }
    init();
});
myApp.controller('adminModalCtrl', ['$scope', '$uibModalInstance', 'info', '$serAdmin','toaster','$localStorage', function($scope, $uibModalInstance, info,$serAdmin, toaster,$localStorage) {
    $scope.sel = {};
    $serAdmin.select({
        token : $localStorage.userInfo&&$localStorage.userInfo.token
    }).then(function(res){
        if(res.code==1){
            $scope.sel = res.data;
        }
    });
    $scope.info = info;

    $scope.ok = function () {

        if(!$scope.info.name
            &&!$scope.info.phone
            &&!$scope.info.role){
            toaster.warning('警告','数据未填');
            return false;
        }

        if(!$scope.info.state&&!$scope.info.password){
            toaster.warning('警告','密码未填');
            return false;
        }
        $uibModalInstance.close($scope.info);
    };

    $scope.cancel = function () {
        $uibModalInstance.dismiss(0);
    };
}]);